/*
* Class: RssFeedsDB
* Description: Class is used as a Database of RssFeed Objects. 
*
* Note:
* Date: March 28, 2012
* Author: Shane Swayney
*/

package rssreaderapp;

import java.io.*;
import java.util.ArrayList;
import java.util.Iterator;

/**
 * Class is used as a Database of RssFeed Objects. 
 * 
 * @author Shane Swayney <shane.swayney@gmail.com>
 */
public class RssFeedsDB {
    
    //Container used as container to hold RssFeeds
    private ArrayList<RssFeed> rssFeedsArrayList;
    
    //Used to specify file savae name
    private final String SAVE_FILE_NAME = "RssFeedsDB.sav";

    
    /**
     * constructor 
     * 
     */
    public RssFeedsDB() {
        this.rssFeedsArrayList = new ArrayList<>();
    }
    
    
    /**
     * Adds an RssFeed to the database
     * 
     * @param rssFeed the {@link RssFeed} to be added
     */
    public void addRssFeed(RssFeed rssFeed)
    {
       rssFeedsArrayList.add(rssFeed);
    }
    
    
    /**
     * Returns the {@link RssFeed} associated with the param title string
     * 
     * @param title of the {@ RssFeed} to be returned
     * @return a RssFeed if found and Null if not.
     */
    public RssFeed getRssFeedByTitle(String title)
    {
        for(RssFeed feed: rssFeedsArrayList)
        {        
            if(feed.getRssTitle().equalsIgnoreCase(title)){
                return feed;
            }
        }
       return null;
    }
    
    
    /**
     * Returns the {@ RssFeed} associated with the url String
     * 
     * @param URL a String containing the url of the feed
     * @return a RssFeed if found and Null if not
     */
    public RssFeed getRssFeedByURL(String URL)
    {
        for(RssFeed feed: rssFeedsArrayList)
        {        
            if(feed.getRssURL().equalsIgnoreCase(URL)){
                return feed;
            }
        }
       return null;
    }
    
    
    /**
     * Removes the RssFeed associated with the param String URL
     * 
     * @param URL the String containing the URL of the RssFeed to remove
     * @return True if removed and False if not
     */
    public boolean removeRssFeedByURL(String URL)
    {
        for(RssFeed feed: rssFeedsArrayList)
        {        
            if(feed.getRssURL().equalsIgnoreCase(URL)){
                rssFeedsArrayList.remove(feed);
                return true;
            }
        }
        return false;
    }
    
    
    /**
     * Removes an RssFeed associated with the param String title
     * 
     * @param title a String containing the title of the RssFeed to be removed
     * @return True if RssFeed was removed and False if not
     */
    public boolean removeRssFeedByTitle(String title)
    {
        for(RssFeed feed: rssFeedsArrayList)
        {        
            if(feed.getRssTitle().equalsIgnoreCase(title)){
                rssFeedsArrayList.remove(feed);
                return true;
            }
        }
        return false;
    }
    
    
    /**
     * Removes all rssFeeds 
     */
    public void removeAllRssFeeds()
    {
        rssFeedsArrayList.clear();
    }
    
    /**
     * Method to check if the Database is Empty
     * @return True if it is empty and false if not
     */
    public boolean isEmpty()
    {
        return rssFeedsArrayList.isEmpty();
    }
    
    
    /**
     * Returns true if {@link RssFeed} is in DB, false otherwise 
     * @param feed an RssFeed to be checked
     * @return True if the RssFeed is in the database, False otherwise
     */
    public boolean containsFeed(RssFeed feed)
    {
        return rssFeedsArrayList.contains(feed);  
    }
    
    
    /**
     * Returns true if an {@link RssFeed} containing the url is in the database
     * 
     * @param feedURL a String contain the url to be checked
     * @return True if it is in the DB and False if not.
     */
    public boolean containsFeedWithURL(String feedURL)
    {
        Iterator<RssFeed> iterator = rssFeedsArrayList.iterator();
        while(iterator.hasNext()){
            
            if(iterator.next().getRssTitle().equalsIgnoreCase(feedURL)){
                return true;
            }
        }
        return false;
      }
    
    
    /**
     * Gets an Iterator to be used to iterate through RssFeed database
     * 
     * @return an Iterator<RssFeed> 
     */
    public Iterator<RssFeed> getFeedsIterator()
    {
        return rssFeedsArrayList.iterator();
    }
    
    /**
     * Method to get an ArrayList of all RssFeed Objects in the database
     * 
     * @return an ArraList<Object> containing all RssFeed Objects in the database
     */
    public ArrayList<Object> getAllFeedTitles()
    {
        //if the DB is not empty create the Arraylist,fill it and return
        if(!this.isEmpty())
        {
            ArrayList<Object> allTitles = new ArrayList<>();
            for(RssFeed feed: rssFeedsArrayList)
            {
                allTitles.add(feed.getRssTitle());
            }
            return allTitles;
        }
        
        //there are no feeds so return empty arraylist
        return new ArrayList<>();
    }
    
    
    /**
     * Loads serialized rssFeedArrayList from file 
     * 
     * @throws FileNotFoundException
     * @throws IOException
     * @throws ClassNotFoundException
     */
    public void loadDB() throws FileNotFoundException, IOException, ClassNotFoundException
    {
            //Create File object to test if there is a save file.
            File testFile = new File(SAVE_FILE_NAME);
            
            //If there is a save load it
            if(testFile.exists())
            {    
                //Create a file input stream
                FileInputStream fileStream = new FileInputStream(SAVE_FILE_NAME);
                
                //Try with resourses... Netbeans insisted
                try (ObjectInputStream os = new ObjectInputStream(fileStream)) 
                {
                    rssFeedsArrayList = (ArrayList<RssFeed>)os.readObject();
                }
            }          
    }
    
    
    /**
     * Saves rssFeedArrayList to file using serialization
     * 
     * @throws FileNotFoundException
     * @throws IOException
     */
    public void saveDB() throws FileNotFoundException, IOException
    {
    
        FileOutputStream fileStream = new FileOutputStream(SAVE_FILE_NAME);
            
        try (ObjectOutputStream os = new ObjectOutputStream(fileStream)) 
        {
            os.writeObject(rssFeedsArrayList);
        }
        
    }

               
}
